#pragma once
#include<stdint.h>
#include<iostream>
#include<string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
using namespace std;
class UDPservice
{
public:
    UDPservice(const string &s,const uint16_t port)
    :_ip(s)
    ,_port(port)
    {}
    int Socket()
    {
        _socketfd = socket(AF_INET,SOCK_DGRAM,0);
        
        return _socketfd;
    }
    int Bind()
    {
        struct sockaddr_in local;
        memset(&local,0,sizeof(local));
        local.sin_family = AF_INET;
        local.sin_port = htons(_port);
        local.sin_addr.s_addr = inet_addr(_ip.c_str());
        return bind(_socketfd,(const sockaddr*)&local,sizeof(local));
    }
    ssize_t Recvfrom(void*buff,size_t size,int flag,struct sockaddr*sock,socklen_t *len)
    {
        return recvfrom(_socketfd,buff,size,flag,sock,len);
    }
    ssize_t Sendto(void*buff,size_t size,int flag,const struct sockaddr*sock,socklen_t len)
    {
        return sendto(_socketfd,buff,size,flag,sock,len);
    }
private:
    int _socketfd;
    uint16_t _port;
    string _ip;
};